package com.ayoka.parenting.ui.group.detail;

import android.app.Activity;
import android.arch.lifecycle.Observer;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.ayoka.parenting.R;
import com.ayoka.parenting.adapter.CircleGroupTopicListAdapter;
import com.ayoka.parenting.bean.CircleGroupBean;
import com.ayoka.parenting.bean.PagerBean;
import com.ayoka.parenting.bean.TopicBean;
import com.ayoka.parenting.bean.UserManager;
import com.ayoka.parenting.databinding.ActivityGroupDetailBinding;
import com.ayoka.parenting.mvp.MVPBaseRecyclerViewActivity;
import com.ayoka.parenting.ui.group.manager.MemberManagerActivity;
import com.ayoka.parenting.ui.group.report.TodayReportTableActivity;
import com.ayoka.parenting.ui.group.topic.detail.TopicDetailActivity;
import com.ayoka.parenting.ui.group.topic.publish.PublishTopicActivity;
import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.BaseViewHolder;
import com.jincp.common.model.ToolBarOptions;
import com.jincp.common.utils.LiveDataBus;
import com.jincp.common.utils.StringUtil;
import com.jincp.image.load.ImageLoader;
import com.jincp.image.load.ImageLoaderOptions;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: GroupDetailActivity.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class GroupDetailActivity extends MVPBaseRecyclerViewActivity<TopicBean, ActivityGroupDetailBinding, GroupDetailContract.View, GroupDetailPresenter> implements GroupDetailContract.View {

    public static void start(Activity activity, String  circleGroupId, String  circleGroupOwnerId, String  circleGroupTitle) {
        Intent intent = new Intent(activity, GroupDetailActivity.class);
        intent.putExtra("CircleGroupId", circleGroupId);
        intent.putExtra("CircleGroupOwnerId", circleGroupOwnerId);
        intent.putExtra("CircleGroupTitle", circleGroupTitle);
        activity.startActivityForResult(intent, REQUEST_CODE_FOR_RESULT);
    }

    private String circleGroupId;
    private String circleGroupOwnerId;
    private String circleGroupTitle;
    private CircleGroupBean circleGroupBean;

    @Override
    protected void configIntentValue() {
        Intent intent = getIntent();
        if (intent != null) {
            circleGroupId = intent.getStringExtra("CircleGroupId");
            circleGroupOwnerId = intent.getStringExtra("CircleGroupOwnerId");
            circleGroupTitle = intent.getStringExtra("CircleGroupTitle");
        }
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_group_detail;
    }

    @Override
    protected void initViews() {
        super.initViews();
        mDataBinding.btnSubscribe.setOnClickListener(this);
        mDataBinding.btnCancelSubscribe.setOnClickListener(this);
        mDataBinding.tvPublishTopic.setOnClickListener(this);
    }

    @Override
    protected SwipeRefreshLayout getRefreshLayout() {
        return mDataBinding.swipeRefresh;
    }

    @Override
    protected RecyclerView getRecyclerView() {
        return mDataBinding.recyclerView;
    }

    @NonNull
    @Override
    protected BaseQuickAdapter<TopicBean, BaseViewHolder> getAdapter() {
        CircleGroupTopicListAdapter adapter = new CircleGroupTopicListAdapter(circleGroupOwnerId);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, final View view, int position) {
                TopicBean topicBean = (TopicBean) adapter.getItem(position);
                if (view.getId() == R.id.btn_operate) {
                    if (mPresenter != null) {
                        mPresenter.showCircleOperatorPopupWindow(GroupDetailActivity.this, view, circleGroupOwnerId, topicBean, position);
                    }
                } else if (view.getId() == R.id.tv_comment) {
                    enterDetailActivity(topicBean);
                } else if (view.getId() == R.id.tv_praise) {
                    if (topicBean.IsLike == 1) {
                        mPresenter.cancelPraiseTopic(topicBean.Id, position);
                    } else {
                        mPresenter.praiseTopic(topicBean.Id, position);
                    }
                } else if (view.getId() == R.id.tv_content) {
                    enterDetailActivity(topicBean);
                } else if (view.getId() == R.id.layout_content_container) {
                    enterDetailActivity(topicBean);
                }
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                TopicBean topicBean = (TopicBean) adapter.getItem(position);
                enterDetailActivity(topicBean);
            }
        });
        return adapter;
    }

    private void enterDetailActivity(TopicBean topicBean) {
        TopicDetailActivity.start(GroupDetailActivity.this ,circleGroupOwnerId, topicBean);
    }

    @Override
    protected void getData(boolean isShowWaitDialog) {
        if (mPresenter != null) {
            if (circleGroupBean == null) {
                mPresenter.getCircleGroupDetail(circleGroupId);
            }
            mPresenter.getCircleGroupTopicListData(isShowWaitDialog, circleGroupId, pageNo, pageSize);
        }
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ToolBarOptions options = new ToolBarOptions(circleGroupTitle);
        setCustomToolBar(mDataBinding.customToolbar.toolbar, options);
        LiveDataBus.getInstance().with("Topic", String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String value) {
                if (!TextUtils.isEmpty(value) && adapter != null) {
                    ((CircleGroupTopicListAdapter)adapter).setNewPublishTopicId(value);
                }
                onRefresh();
            }
        });
        getData(true);
    }

    @Override
    protected GroupDetailPresenter getPresenter() {
        return new GroupDetailPresenter();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == mDataBinding.btnSubscribe) {
            if (mPresenter != null) {
                mPresenter.joinCircle(circleGroupId);
            }
        } else if (view == mDataBinding.btnCancelSubscribe) {
            if (mPresenter != null) {
                mPresenter.quitCircle(circleGroupId);
            }
        } else if (view == mDataBinding.tvPublishTopic) {
            if (circleGroupBean != null && circleGroupBean.JoinedStatus == 0) {
                showToast("你还未关注圈子，不能发表心得");
                return;
            }
            PublishTopicActivity.start(this, circleGroupId);
        }
    }

    @Override
    public void configCircleGroupDetail(CircleGroupBean data) {
        if (data == null) return;
        this.circleGroupBean = data;
        if (circleGroupBean.Founder != null && circleGroupBean.Founder.LocalAvatar != null) {
            ImageLoader.getInstance().displayImage(circleGroupBean.Founder.LocalAvatar, mDataBinding.ivCoachHeader, ImageLoaderOptions.getCircleOptions());
        }
        mDataBinding.tvTitle.setText(circleGroupBean.Title);
        mDataBinding.tvSubscribeCount.setText(StringUtil.formatString(R.string.format_subscribe_count, circleGroupBean.MemberCount));
        if (!TextUtils.equals(UserManager.getInstance().uid, circleGroupBean.FounderUid)) {
            mDataBinding.btnSubscribe.setVisibility(circleGroupBean.JoinedStatus == -1 ? View.VISIBLE : View.GONE);
            mDataBinding.btnWaitAgree.setVisibility(circleGroupBean.JoinedStatus == 0 ? View.VISIBLE : View.GONE);
            mDataBinding.btnCancelSubscribe.setVisibility(circleGroupBean.JoinedStatus > 0 ? View.VISIBLE : View.GONE);
            mDataBinding.tvPublishTopic.setVisibility(circleGroupBean.JoinedStatus > 0 ? View.VISIBLE : View.GONE);
        }
    }

    @Override
    public void configCircleGroupTopicListData(PagerBean<TopicBean> data) {
        if (data == null) return;
        setLoadMoreLayout(data.list, data.totalPage);
    }

    @Override
    public void onJoinCircleCallback(boolean isSuccess) {
        if (isSuccess) {
            circleGroupBean.JoinedStatus = 1;
            mDataBinding.btnSubscribe.setVisibility(View.GONE);
            mDataBinding.btnCancelSubscribe.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onQuitCircleCallback(boolean isSuccess) {
        if (isSuccess) {
            circleGroupBean.JoinedStatus = 0;
            mDataBinding.btnSubscribe.setVisibility(View.VISIBLE);
            mDataBinding.btnCancelSubscribe.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPraiseTopicCallback(boolean isSuccess, int position) {
        if (isSuccess) {
            TopicBean topicBean = adapter.getItem(position);
            topicBean.IsLike = 1;
            topicBean.LikeCount++;
            adapter.notifyItemChanged(position);
        }
    }

    @Override
    public void onCancelPraiseTopicCallback(boolean isSuccess, int position) {
        if (isSuccess) {
            TopicBean topicBean = adapter.getItem(position);
            topicBean.IsLike = 0;
            topicBean.LikeCount--;
            adapter.notifyItemChanged(position);
        }
    }

    @Override
    public void onDeleteTopicCallback(boolean isSuccess, int position) {
        if (isSuccess) {
            adapter.remove(position);
        }
    }

    @Override
    public void onTopTopicCallback(boolean isSuccess, int position) {
        if (isSuccess) {
            onRefresh();
        }
    }

    @Override
    public void onCancelTopTopicCallback(boolean isSuccess, int position) {
        if (isSuccess) {
            onRefresh();
        }
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (TextUtils.equals(UserManager.getInstance().uid, circleGroupOwnerId)) {
            getMenuInflater().inflate(R.menu.menu_group_detail, menu);//加载menu文件到布局
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
//        if (item.getItemId() == R.id.action_report) {
//            TodayReportTableActivity.start(this, circleGroupId, circleGroupTitle);
//        }
        if (item.getItemId() == R.id.action_member_manager) {
            MemberManagerActivity.start(this, circleGroupId);
        }
        return true;
    }
}
